
#include "cumsum_tiling.h"
#include "tiling/platform/platform_ascendc.h"
#include "register/op_def_registry.h"


namespace optiling {
const uint32_t BLOCK_SIZE = 32;
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

    CumsumTilingData tiling;
    uint32_t NUM = 8;   //
    uint32_t sizeofdatatype;
    uint32_t totalLengthAligned;
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    auto socVersion = ascendcPlatform.GetSocVersion();
    uint64_t ub_size;
    ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ub_size);
    auto aivNum = ascendcPlatform.GetCoreNumAiv();

    
    uint32_t blockLength = context->GetInputTensor(0)->GetShapeSize();

    auto shape = context->GetInputTensor(0)->GetShape().GetOriginShape();
    auto dim_num = shape.GetDimNum();

    uint32_t Inputshape[10] = {0};
    for(int32_t i = 0; i < dim_num; i++)
    {
        Inputshape[i] = shape.GetDim(i);
    }
    

    bool exclusive = *context->GetAttrs()->GetBool(0);
    bool reverse = *context->GetAttrs()->GetBool(1);

    tiling.set_exclusive(exclusive);
    tiling.set_reverse(reverse);


    auto dt = context->GetInputTensor(0)->GetDataType();
    if(dt == 0)     // float32
    {   
        sizeofdatatype = 4;
        NUM = 4;
        context->SetTilingKey(1);
    } else if (dt == 1)     //float16
    {  
        sizeofdatatype = 2;
        context->SetTilingKey(2);
    } else if (dt == 2)     //int8
    {   
        sizeofdatatype = 1;
        context->SetTilingKey(4);
    } else  //int32
    { 
        sizeofdatatype = 4;
        NUM = 4;
        context->SetTilingKey(3);
    }

    uint32_t ALIGN_NUM = BLOCK_SIZE / sizeofdatatype;

    //元素总量 32Byte对齐
    blockLength = (blockLength + ALIGN_NUM - 1) / ALIGN_NUM * ALIGN_NUM;    

    uint32_t tiling_size = ((ub_size) / BLOCK_SIZE / 2) / NUM;
    //一次tiling的最大数据
    uint32_t block_size = tiling_size * ALIGN_NUM;


    context->SetBlockDim(1); 

    tiling.set_shape(Inputshape);
    tiling.set_blockLength(blockLength);
    tiling.set_dimNum(dim_num);
    tiling.set_blockSize(block_size);
    tiling.set_ALIGN_NUM(ALIGN_NUM);
    

    // printf("ub_size: %u\n", ub_size);
    // printf("blockLength: %u\n", blockLength);
    // printf("dim_num: %u\n", dim_num);
    // printf("block_size: %u\n", block_size);
    // printf("ALIGN_NUM: %u\n", ALIGN_NUM);
    // printf("exclusive: %d\n", exclusive);
    // printf("reverse: %d\n", reverse);


    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    size_t *currentWorkspace = context->GetWorkspaceSizes(1);
    currentWorkspace[0] = 0;
    return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class Cumsum : public OpDef {
public:
    explicit Cumsum(const char* name) : OpDef(name)
    {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_INT8})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("axis")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_INT8})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("exclusive").AttrType(OPTIONAL).Bool(false);
        this->Attr("reverse").AttrType(OPTIONAL).Bool(false);

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(Cumsum);
}
